package com.mosai.smartmedical.util;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.mosai.alignpro.entity.ImageInfo;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Created by Rays on 2020/3/20.
 */
public final class DataUtils {

    public static String getRnParamByWaitingNoPatient(Context context) {
        return getJson(context, "rnParamByWaitingNoPatient.json");
    }

    public static String getRnParamByWaiting(Context context) {
        return getJson(context, "rnParamByWaiting.json");
    }

    public static String getRnParamByAudited(Context context) {
        return getJson(context, "rnParamByAudited.json");
    }

    public static String getRnParamByPatientDetail(Context context) {
        return getJson(context, "rnParamByPatientDetail.json");
    }

    public static String getRnParamByPatientApp(Context context) {
        return getJson(context, "rnParamByPatientApp.json");
    }

    public static List<ImageInfo> getImageList(Context context) {
        String json = getJson(context, "imageList.json");
        return JSON.parseArray(json, ImageInfo.class);
    }

    public static String getJson(Context context, String fileName) {
        StringBuilder sb = new StringBuilder();
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader bufferedReader = null;
        try {
            is = context.getAssets().open(fileName);
            isr = new InputStreamReader(is);
            bufferedReader = new BufferedReader(isr);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(bufferedReader, isr, is);
        }
        return sb.toString();
    }

    public static void close(Closeable... closeables) {
        for (Closeable closeable : closeables) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
